package com.ttdc.controller;

import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ttdc.pojo.OrderList;
import com.ttdc.pojo.OrderListExample;
import com.ttdc.service.OrderListService;

//@EnableAutoConfiguration
@Controller
public class OrderListController {

	@Autowired
	private OrderListService orderListService;
	private OrderListExample example;

	// 删除
	@RequestMapping("/or_delete")
	public String deleteByPrimaryKey(@RequestParam("olId") Integer olId, Model model) {
		orderListService.deleteByPrimaryKey(olId);
		return "redirect:/or_list";
	}

	// 增加
	@SuppressWarnings("finally")
	@RequestMapping("/or_save")
	public String insert(OrderList record, Model model) {
		try {
			orderListService.insertSelective(record);
			List<OrderList> list = orderListService.selectByAll();
			model.addAttribute("list", list);

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			return "redirect:/or_list";
		}
	}

	// 查询 可多條件查詢
	@RequestMapping("or_selectById")
	public String selectByPrimaryKey(OrderList orderList, Model model) {
		example = new OrderListExample();
		OrderListExample.Criteria c = example.createCriteria();
		if (orderList == null) {
			// model.addAttribute("message","此用户名不存在，请确认后再输入！");
			return "orderlist/oltest";
		} else {
			if (orderList.getOlType() != null) {
				// 根據訂單類型查詢
				System.out.println(orderList.getOlType());

				c.andOlTypeEqualTo(orderList.getOlType());
				List<OrderList> list = orderListService.selectByExample(example);
				model.addAttribute("list", list);
			}
			if (orderList.getTbId() != null) {
				// 根據餐桌號查詢訂單
				System.out.println(orderList.getTbId());
				c.andTbIdEqualTo(orderList.getTbId());
				List<OrderList> lst = orderListService.selectByExample(example);
				model.addAttribute("list", lst);
				
			}
			if (orderList.getOlId() != null) {
				// 根據訂單編號查詢
				System.out.println(orderList.getOlId());
				orderList = orderListService.selectByPrimaryKey(orderList.getOlId());
				model.addAttribute("list", orderList);
				// System.out.println(orderList.toString());
			}
			return "orderlist/oltest";
		}

	}


	// 跳转到修改页面，根据id查出来在update那个页面显示
	@RequestMapping("/or_tocheck")
	public String updateByPrimaryKey(Integer olId, Model model) {
		OrderList list = orderListService.selectByPrimaryKey(olId);
		model.addAttribute("list", list);
		return "orderlist/olupdate";

	}

	// 由update修改页面放回结果进行修改，然后返回列表
	@RequestMapping("/or_check")
	public String updateByPrimaryKeySelective(OrderList record) {
		orderListService.updateByPrimaryKey(record);
		return "redirect:/or_list";

	}

	// 用条件查询所有的订单信息
	@RequestMapping("/or_selectAll")
	public String selectAll(Model model) {
		example = new OrderListExample();
		OrderListExample.Criteria criteria = example.createCriteria();
		criteria.andOlIdIsNotNull();
		List<OrderList> list = orderListService.selectByExample(example);
		model.addAttribute("list", list);
		return "orderlist/oltest";
	}

	// 查询所有数据
	@RequestMapping("/or_list")
	public String selectByAll(Model model) {
		List<OrderList> list = orderListService.selectByAll();
		model.addAttribute("list", list);
		return "orderlist/oltest";
	}
}
